<template>
  <div class="content">
    <van-nav-bar
      @click-left="$router.back()"
      left-arrow
      :title="$route.query.msg == 2 ? '发布视频' : '推广视频'"
      fixed
    >
      ></van-nav-bar
    >

    <main>
      <h3 v-if="$route.query.msg == 2">
        <img src="../../assets/images/detail/head.png" alt="" />
        发布统计
      </h3>
      <h3 v-else>
        <img src="../../assets/images/detail/head.png" alt="" />
        推广统计
      </h3>
      <div class="info" v-if="$route.query.msg == 2">
        <div class="item">
          <h4>已发布视频数</h4>
          <p>
            {{ tasks.publish || 0 }}
            <span>篇</span>
          </p>
        </div>
        <div class="item">
          <h4>累计播放数</h4>
          <p>{{ tasks.read || 0 }} <span>次</span></p>
        </div>
        <div class="item">
          <h4>累计转发数</h4>
          <p>{{ tasks.transpond || 0 }}<span>次</span></p>
        </div>
      </div>
      <div class="info" v-else>
        <div class="item">
          <h4>可推广视频数</h4>
          <p>
            {{ tasks.publish || 0 }}
            <span>篇</span>
          </p>
        </div>
        <div class="item">
          <h4>剩余赏金</h4>
          <p>{{ tasks.moneyReward || 0 }} <span>元</span></p>
        </div>
        <div class="item">
          <h4>已发出赏金</h4>
          <p>{{ tasks.transmit || 0 }}<span>元</span></p>
        </div>
      </div>
      <van-sticky offset-top="46">
        <h3>
          <img src="../../assets/images/detail/head.png" alt="" />
          视频列表
          <p v-if="$route.query.msg == 2" @click="goAdd">新建视频></p>
        </h3>

        <van-search
          shape="round"
          placeholder="搜索视频标题"
          v-model="title"
          @input="searchKey"
        />
        <div class="sort" v-if="$route.query.msg != 2">
          <div class="item" @click="pressSort(1)">
            按单价排序
            <img
              v-if="sortType == 1 && sort == 1"
              src="../../assets/images/task/search_max.png"
              alt=""
              class="imgs"
            />
            <img
              v-else-if="sortType == 1 && sort == 0"
              src="../../assets/images/task/search_min.png"
              alt=""
              class="imgs"
            />
            <img
              v-else
              src="../../assets/images/task/search_no.png"
              alt=""
              class="imgs"
            />
          </div>
          <div class="item" @click="pressSort(0)">
            按发布时间排序
            <img
              v-if="sortType == 0 && sort == 1"
              src="../../assets/images/task/search_max.png"
              alt=""
              class="imgs"
            />
            <img
              v-else-if="sortType == 0 && sort == 0"
              src="../../assets/images/task/search_min.png"
              alt=""
              class="imgs"
            />
            <img
              v-else
              src="../../assets/images/task/search_no.png"
              alt=""
              class="imgs"
            />
          </div>
          <div class="item" @click="pressSort(2)">
            按剩余次数排序
            <img
              v-if="sortType == 2 && sort == 1"
              src="../../assets/images/task/search_max.png"
              alt=""
              class="imgs"
            />
            <img
              v-else-if="sortType == 2 && sort == 0"
              src="../../assets/images/task/search_min.png"
              alt=""
              class="imgs"
            />
            <img
              v-else
              src="../../assets/images/task/search_no.png"
              alt=""
              class="imgs"
            />
          </div>
        </div>
      </van-sticky>
      <div class="list" v-if="videoArr.length != 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          :immediate-check="false"
        >
          <div
            class="item"
            v-for="(item, index) in videoArr"
            :key="index"
            @click.prevent="$router.push(`/previewVideo?id=${item.id}`)"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9 || item.type == 10"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.describes }}</span>
              <div class="bottom">
                <div class="bleft" v-if="$route.query.msg == 2">
                  <div class="check">
                    <img
                      v-if="item.type !== 8 && item.audit == 2"
                      src="../../assets/images/video/checked.png"
                      alt=""
                    />
                    <img
                      v-else-if="item.type !== 8 && item.audit == 1"
                      src="../../assets/images/video/checkno.png"
                      alt=""
                    />
                  </div>
                  <div class="uped">
                    <img
                      src="../../assets/images/video/up.png"
                      v-if="item.status == 2"
                      alt=""
                    />

                    <img
                      src="../../assets/images/video/noup.png"
                      alt=""
                      v-else
                    />
                  </div>
                </div>
                <div class="Aleft" v-if="$route.query.msg == 1">
                  <span>奖金额：{{ item.spreadPrice || 0 }}元/次</span>
                  <span>剩余次数：{{ item.residueNum || 0 }}次</span>
                </div>
                <!-- <div
                class="rights"
                v-if="$route.query.msg == 2"
                @click.stop="clickAction(item)"
              >
                <van-icon name="ellipsis" />
              </div> -->
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 空状态 -->
      <van-empty
        description="当前暂无视频内容~"
        class="custom-image"
        :image="require('../../assets/dafault/novideo.png')"
        v-else
      >
        <van-button round type="danger" class="bottom-button" @click="goAdd">
          去新建视频
        </van-button>
      </van-empty>
      <!-- 弹框 -->
      <van-action-sheet
        v-model="showAction"
        :actions="actions"
        cancel-text="取消"
        close-on-click-action
        @cancel="showAction = false"
        @select="select"
      />
      <!-- 删除弹框 -->
      <van-dialog v-model="showDel" :show-confirm-button="false">
        <template #title>
          <van-icon name="cross" @click="showDel = false" />
        </template>
        <template #default>
          <div class="cont">
            <p>确定要删除该视频？</p>
            <van-divider />
            <div class="foot">
              <div class="left" @click="showDel = false">取消</div>
              <div class="right" @click="deleteArticlesEtc">确定</div>
            </div>
          </div>
        </template>
      </van-dialog>
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "", //搜素的视频标题
      videoArr: [], //视频列表
      page: 1, //当前页数,
      agentId: "",
      wxUserInfo: {}, //微信用户个人信息
      tabelType: 1,
      tasks: {}, //转发任务数量
      swiperArr: [], //轮播图
      checked: true,
      sortType: "", //排序
      sort: "", //从小到大
      isPower: 2,
      info: "",
      showAction: false, //弹框

      showDel: false, //删除弹框
      itemObj: {}, //选择的项
      loading: false, //加载状态
      finished: false, //完成
      page: 1, //页数
      limit: 8, //条数
      totalPage: "", //总页数
    };
  },

  async mounted() {
    this.wxUserInfo = JSON.parse(sessionStorage.getItem("userWxData"));

    this.agentId = sessionStorage.getItem("agentId");

    // 获取个人信息
    await this.$store.dispatch("updateUserInfo");
    if (this.$route.query.msg == 2) {
      this.getReleasedStatistics();
    } else {
      this.getPromotionStatistics();
    }

    this.getVideoList();
    this.isCustomer();
  },
  computed: {
    actions() {
      let arr = [
        { name: this.itemObj.status == 1 ? "上架视频" : "下架视频", id: 2 },
        { name: "预览视频", id: 3 },
        { name: "修改视频", id: 4 },
        { name: "删除视频", id: 5 },
      ];
      if (this.itemObj.type == 8) {
        arr.unshift({ name: "推广视频", id: 1 });
      }
      return arr;
    },
  },
  methods: {
    //触底下一页
    onLoad() {
      if (this.page >= this.totalPage) {
        this.finished = true;
        return;
      }
      this.page++;

      this.getVideoList();
      // 加载状态结束
      this.loading = false;
    },
    //删除
    async deleteArticlesEtc() {
      let obj = {
        id: this.itemObj.id,
        tableType: 3,
        userId: this.$store.state.userInfo.id,
      };
      let res = await this.$api.deleteArticlesEtc(obj);
      if (res.code != 0) return this.$toast(res.msg);
      this.showDel = false;
      await this.getVideoList();
      this.$toast("删除成功");
    },
    //点击触发选择弹框
    clickAction(item) {
      this.itemObj = item;
      this.showAction = true;
    },
    //选择
    select(e) {
      if (e.id == 5) {
        //删除
        if (this.isRoot() == 2) {
          return this.$toast("您的权限不足");
        }
        this.showDel = true;
      } else if (e.id == 4) {
        //修改视频
        if (this.isRoot() == 2) {
          return this.$toast("您的权限不足");
        }
        this.$router.push(`/addVideo?id=${this.itemObj.id}&title=修改`);
      } else if (e.id == 2) {
        // 上下架
        this.updateStatus(this.itemObj.status, this.itemObj.id);
      } else if (e.id == 3) {
        // 预览
        this.$router.push("/previewVideo?id=" + this.itemObj.id);
      } else if (e.id == 1) {
        //推广文章
        this.$router.push(`/addVideo?id=${this.itemObj.id}&title=推广`);
      }
    },
    //去创建
    goAdd() {
      if (this.isPower == 3 && this.info != " ") {
        return this.$router.push("/mineInstructions");
      } else if (this.isPower == 3 && this.info == " ") {
        return this.$toast("您的权限不足！");
      }
      if (this.isPower) {
        this.$router.push({ path: "/addVideo" });
      }
    },
    //判断是否有权限创建
    async isCustomer() {
      let account = sessionStorage.getItem("agentId");
      let res = await this.$api.isCustomer({
        openId: this.wxUserInfo.openid,
        account,
        id: this.$store.state.userInfo.id,
        phone: this.$store.state.userInfo.phone,
      });
      this.isPower = res.data;
      this.info = res.info;
    },
    // 判断是否有权限删除修改等
    async isRoot() {
      let account = sessionStorage.getItem("agentId");
      let res = await this.$api.isCustomer({
        openId: this.wxUserInfo.openid,
        account,
        id: this.$store.state.userInfo.id,
        phone: this.$store.state.userInfo.phone,
        type: 1,
        tableType: 1,
        tableId: this.itemObj.id,
      });
      return res.data;
    },
    // 按剩余次数 按发布时间 按单价 排序
    pressSort(n) {
      if (this.sortType == n) {
        if (this.sort == 0) {
          this.sort = 1;
        } else {
          this.sort = 0;
        }
      } else {
        this.sortType = n;
      }

      this.getVideoList();
    },
    //搜素
    searchKey() {
      clearTimeout(this.time);
      this.time = setTimeout(async (_) => {
        this.getVideoList();

        this.time = null;
      }, 700);
    },
    //上下架
    async updateStatus(status, id) {
      var str;
      if (status == 1) {
        str = "上架";
        status = 2;
      } else {
        str = "下架";
        status = 1;
      }
      let obj = {
        tableType: 4,
        status,
        id,
        type: 2, //上下架 1审核2上下架
      };
      let res = await this.$api.updateStatus(obj);
      if (this.$route.query.msg == 2) {
        this.getReleasedStatistics();
      } else {
        this.getPromotionStatistics();
      }
      if (res.code != 0) return this.$toast(res.msg);
      this.getVideoList();
      this.$toast(str + "成功");
    },

    //获取视频列表
    async getVideoList() {
      let obj = {
        account: this.agentId,
        limit: 10000000,
        page: 1,
        tabelType: 4,
        sortType: this.sortType, //排序
        sort: this.sort,
      };
      // 推广
      if (this.$route.query.msg == 1) {
        obj.type = 9;
        obj.status = 2;
      }
      // 发布
      if (this.$route.query.msg == 2) {
        obj.userId = this.$store.state.userInfo.id;
        obj.operation = 1;
      }
      let res = await this.$api.getVideoList(obj);
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.videoArr = res.data.list || [];
      } else {
        this.videoArr.push(...res.data.list);
      }
      this.totalPage = res.data.totalPage;
    },

    //获取发布数据统计
    async getReleasedStatistics() {
      let res = await this.$api.getReleasedStatistics({
        account: this.$store.state.userInfo.account,
        tableType: 4,
        userId: this.$store.state.userInfo.id,
      });
      if (res.code !== 0) return;
      this.tasks = res.data;
    },
    // 推广数据统计
    async getPromotionStatistics() {
      let res = await this.$api.getPromotionStatistics({
        account: this.$store.state.userInfo.account,
        tableType: 4,
        status: 2,
      });
      if (res.code !== 0) return;
      this.tasks = res.data;
    },
  },
};
</script>

<style scoped lang="less">
/deep/.van-empty__image {
  width: 166px;
  height: 139px;
}
/deep/.van-sticky--fixed {
  padding: 0 15px;
  background-color: #fff;
}
.bottom-button {
  width: 130px;
  height: 34px;
  background-color: #477fe6;
  border-radius: 17px;
  border: none;
  font-size: 13px;
  color: #ffffff;
}
main {
  padding: 0 15px;

  padding-top: 46px;

  h3 {
    background-color: #fff;
    font-size: 16px;
    font-weight: 700;
    color: #333333;
    letter-spacing: 1px;
    margin: 20px 0 10px;
    display: flex;
    align-items: center;
    img {
      width: 4px;
      height: 15px;
      margin-right: 5px;
    }
    p {
      flex: 1;
      font-size: 12px;
      text-align: right;
      color: #3b447a;
    }
  }

  .info {
    display: flex;
    justify-content: space-between;
    .item {
      width: 110px;
      height: 63px;
      border-radius: 5px;
      color: #fff;
      border-radius: 10px;
      background: url("../../assets/images/detail/bg1.png") no-repeat;
      background-size: 100%;
      &:nth-of-type(2) {
        background: url("../../assets/images/detail/bg2.png") no-repeat;
        background-size: 100%;
      }
      &:nth-of-type(3) {
        background: url("../../assets/images/detail/bg3.png") no-repeat;
        background-size: 100%;
      }

      h4 {
        font-size: 11px;
        padding: 8px 0;
        text-align: center;
      }
      p {
        font-size: 19px;
        font-weight: 700;
        display: flex;
        justify-content: center;
        align-items: center;
        span {
          font-size: 10px;
          font-weight: normal;
        }
      }
    }
  }

  .sort {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 20px;
    .item {
      font-size: 11px;
      text-align: left;
      color: #666666;
      .imgs {
        width: 8px;
        height: 100%;
      }
    }
  }
  .list {
    padding-bottom: 50px;
    .item {
      display: flex;
      margin-top: 12px;
      .left {
        width: 90px;
        height: 90px;
        margin-right: 15px;
        position: relative;
        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
        .money {
          width: 35px;
          height: 35px;
          position: absolute;
          right: 0;
          top: 0;
          border-radius: none;
        }
      }
      .right {
        flex: 1;

        span {
          font-size: 11px;
          color: #999999;
        }
        .title {
          margin-top: 5px;
          display: block;
          font-size: 14px;
          color: #333333;
        }
        .p {
          margin-top: 3px;
          height: 30px;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
        }
        .bottom {
          width: 246px;
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
          .bleft {
            display: flex;
            .check {
              img {
                width: 39px;
                height: 13px;
              }
            }
            .uped {
              margin-left: 11px;
              img {
                width: 29px;
                height: 12px;
                border-radius: 2px;
              }
            }
          }
          .Aleft {
            display: flex;
            justify-content: space-between;
            width: 185px;
          }
          .rights {
            width: 30px;
            text-align: center;
          }
        }
      }
    }
  }

  .btn {
    width: 146px;
    height: 42px;
    opacity: 1;
    background: #477fe6;
    border-radius: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    margin: 15px auto;
    font-size: 13px;
    img {
      width: 16px;
      height: 16px;
      margin-right: 6px;
    }
  }
}
</style>
